-
-
Notifications
You must be signed in to change notification settings - Fork 670
feat: String interpolation #1427
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
ff3d722
to
7508daa
Compare
7508daa
to
b9f7c0e
Compare
b9f7c0e
to
72e1372
Compare
I was also working on this, before realizing that someone else was. I'll leave it to you, but here are some test cases I came up with while testing my implementation, just in case they're helpful: tests/parser/literals.ts:
tests/parser/literals.fixture.ts (these will probably differ for your implementation)
|
Btw will be great if you avoid using |
thanks, my return parts look like this
|
I guess it should be (in pseudocode): if (parts.lenght == 0) return <empty string>;
else if (parts.lenght == 1) {
return isStringExpression(parts[0])
? parts[0]
: makeCallExpression('toString', parts[0]) // stringify if needed
} else if (parts.lenght == 2) {
return Node.createBinaryExpression(Token.PLUS, parts[0], parts[1], tn.range(startPos, tn.pos));
} else if (parts.lenght == 3) {
// optional for this PR but will be great
// depends on pr #1432
return makeCallExpression('joinThreeStrings', parts[0], parts[1], parts[2]);
} else {
return makeCallExpression('joinStringArray', makeArrayExpression(parts));
} |
@MaxGraey According to the pseudocode
// isStringExpression checks if the string is an Expression
function isStringExpression(expr: any): boolean {
return (expr instanceof Expression) ? true : false
}; // makeCallExpression calls an expression according to the function
function makeCallExpression(callExpression: string , ...args:any) {
switch (callExpression) {
case 'toString':
return (typeof args == 'object') ? JSON.stringify(args) : args.toString()
break;
case 'joinThreeStrings':
return joinThreeStrings(args[0], args[1], args[2]);
break;
case 'joinStringArray':
return joinStringArray(args);
break;
} function makeArrayExpression(parts: any) {
return {
type: "ArrayExpression",
dataStart: parts[0],
length: parts[1],
separator: parts[2]
};
} |
No, this will much more complicated. I guess @dcodeIO will assist better than me how to implement such routines |
To emit a call to a standard library function, one would obtain a reference to it from the program, resolve it and create a direct call: let prototype = program.require("~lib/file/concat3", ElementKind.FUNCTION_PROTOTYPE);
let instance = assert(resolver.resolveFunction(<FunctionPrototype>prototype, null)); // here: not generic
let expr = compiler.makeCallDirect(instance, [arg1, arg2, arg3], reportNode, immediatelyDropped?, skipAutorelease?); emitting about (call $~lib/file/concat3
(arg1)
(arg2)
(arg3)
) |
@dcodeIO Thank you so much! |
Already implemented in #1715. Closing |
base #1115
close near/near-sdk-as#201